﻿package Naruto.Frames {	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	import flash.display.MovieClip; 
	import flash.display.Sprite;
	import flash.display.Stage;
	
	import Naruto.Bloods.*;
	import Naruto.*;

		
	public class Frame4 extends Frame{
		private const ENEMYSNUM:int = 10;//敌人数量
		
		public function Frame4(aStatus:Status, stage:Stage, appStart:Start){
			super(aStatus, stage, appStart);
			init();
		}

		private function init():void{
			creatAshman();
			this._loadPic = new LoadPic(this._stage,4);	//加载第四个场景
//			trace(this._status.getHealth());

			me.setpos(this._stage,this._stage.stageWidth*(2/3),this._stage.stageHeight*(1/8));				
			me.setLevel(this._status.getLevel());	
			this._stage.addChild(me.getme());
			creatAiAttack();	//敌人开始攻击
		}
		
		override public function creatEnemys():void{
			enemynum = 0;
			enemys = new Array();
			for(var i=0;i<ENEMYSNUM;i++){
				enemys[i] = new Genin(this._stage);		//开始布置下忍*10
			}
			creataitimer = new Timer(1000);
			creataitimer.addEventListener(TimerEvent.TIMER,CreatAI);
			creataitimer.start();
		}

		override public function CreatAI(event:Event):void{
			if((enemynum<ENEMYSNUM) && (this._blood.getHealth()>0)){
				this._stage.addChild(enemys[enemynum].getme());
				enemystestobj.push(enemys[enemynum].getme());	//将新建敌人加入敌人碰撞队列 （10个）
				enemynum += 1;
			}
			else{
				creataitimer.removeEventListener(TimerEvent.TIMER,CreatAI);
				creataitimer.stop();
			}			
		}
				
		override public function aiAttack(event:Event){
			//敌人攻击
			for(var m=0;m<ENEMYSNUM;m++){
				if(enemys[m]){
					if(enemys[m].getStatus()){
						flyshurtmeobj.push(enemys[m].getAttack().AiStartAttack(this._stage,enemys[m].getme(),me.getme()));	//敌人攻击
					}
				}
			}
		}
		
		override public function changeFrame(changeNum:int = 99){
			switchChangeNum(changeNum,4);
		}

		override public function onEnterFrame(event:Event):void{
			if(this._isDead == false){
				if(this._blood.getHealth()<=0){
					//trace("dead!");
					this._isDead = true;
					this.flag = 1;	//鼠标发镖事件停止。
					new GameOver(missionFailed,6,changeFrame);
				}
			}
			
			//碰撞检测
			flyshittest.flysobjtesting(this._stage,event,enemys,flystestobj,this._appStart); //开始敌人碰撞检测
			flyshittest.flysHurtMeTest(this._stage,event,me,flyshurtmeobj,this._blood);		//开始主角受伤碰撞检测
				
			//如果主角攻击存在且飞镖超出边界，移除忍标 
			if( meattack ){
				var flys1:Flys1 = meattack.getflys1();
				if(flys1){
					if(flag==1){
						if( (flys1.x>=this._stage.stageWidth) || (flys1.y>=this._stage.stageHeight) || (flys1.x<0) || (flys1.y<0) ){
							meattack.removeflys1();
							flys1 = null;
							meattack = null;
							this.flag = 0;
							return;
						}
					}
					var vx:Number = Math.cos(meattack.angle) * meattack.flysspeed;
					var vy:Number = Math.sin(meattack.angle) * meattack.flysspeed;
					flys1.x += vx;
					flys1.y += vy;
				}
			}
			
			//如果敌人攻击存在且飞镖超出边界，移除忍标 ??? 已被上面移除？？
			for(var s=0;s<ENEMYSNUM;s++){
				if(enemys[s]){	//如果敌人存在
					if(enemys[s].getAttack().getflys2()){	//如果忍标存在
						if( this._stage.contains(enemys[s].getAttack().getflys2()) ){	   //如果在舞台上
							var flys2 = enemys[s].getAttack().getflys2();
							if( (flys2.x>=this._stage.stageWidth) || (flys2.y>=this._stage.stageHeight) || (flys2.x<0) || (flys2.y<0) ){	//如果超出了边界
								enemys[s].getAttack().removeflys2();
								flystestobj[s] = null;
							}
						}
					}
				}
			}
			
			//键盘
			for(var k=0;k<keycode.length;k++){
				if(me){
					switch(keycode[k]){
						case 87:if(me.getposy() > 50) {me.changepos("up");break};		//W
						case 83:if(me.getposy() < 770){me.changepos("down");break}; 	//S
						case 65:if(me.getposx() > 50) {me.changepos("left");break}; 	//A
						case 68:if(me.getposx() < 980){me.changepos("right");break};	//D
						
						case 48:{changeItem(0);this._status.setCurrentItem(0);break;}	//0
						case 49:{changeItem(1);this._status.setCurrentItem(1);break;}	//1
					}
				}
			}
		}
	}
}